/***************************************************************************
创建者：华磊
 开始时间：               : 2020.1１.21
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   消息视窗测试用代理类
*                                                                         *
 ***************************************************************************/
#include "messagedelegate.h"
#include <QDebug>


MessageDelegate::MessageDelegate(MessageView *messageViewIn, GlobalData *globalDataIn, QWidget *parent):
        QObject(parent)
{
    globalData = globalDataIn;
    messageView=messageViewIn;

    currentRobotId = -1;
    updateFLag = false;
    errorFlag = 0;
    initConnect();
}

void MessageDelegate::initConnect()
{
    connect(messageView, SIGNAL(clearMessage(int)), this, SLOT(clearMessageSlot(int)));
    connect(messageView, SIGNAL(clearAllMessage()), this, SLOT(clearAllMessageSlot()));

    connect(messageView, SIGNAL(errorMessageShowOrHide(int)), this, SLOT(errorMessageShowOrHideSlot(int)));
    connect(messageView, SIGNAL(warnMessageShowOrHide(int)), this, SLOT(warnMessageShowOrHideSlot(int)));
    connect(messageView, SIGNAL(noteMessageShowOrHide(int)), this, SLOT(noteMessageShowOrHideSlot(int)));
    connect(messageView, SIGNAL(waitMessageShowOrHide(int)), this, SLOT(waitMessageShowOrHideSlot(int)));
    connect(messageView, SIGNAL(showCurrentRobotMessage()), this, SLOT(showCurrentRobotMessageSlot()));
    connect(messageView, SIGNAL(showAllRobotMessage()), this, SLOT(showAllRobotMessageSlot()));
}

/**
 * @brief MessageDelegate::initialView　初始化界面类成员变量
 */
//void MessageDelegate::initialView()
//{

//}


void MessageDelegate::updateDataAndView()
{
    //update数据
    if(globalData)                                      //数据指针存在
    {

//        if(globalData->tryLock())
//        {
            if(globalData->globalLoginStatus)                           //已登陆
            {
                std::vector<CommRobotInfo> robotInfoListOut;
                globalData->getRobotInfoList(robotInfoListOut);
                //获取当前机器人名
                if(currentRobotId != globalData->currentRobotID)
                {
                    currentRobotId = globalData->currentRobotID;
                    messageView->robotID = currentRobotId;
                    //qDebug()<<currentRobotId;
                    for(int i = 0;i<robotInfoListOut.size();i++)
                    {
                        if(currentRobotId == robotInfoListOut[i].robotId)
                        {
                            messageView->robotName = QString::fromStdString(robotInfoListOut[i].robotName);//待定
                            //qDebug()<<messageView->robotName;
                            break;
                        }
                    }
                }

                //获取信息内容
                std::vector<Message> messageVectorOut;
                globalData->getMessageVector(messageVectorOut);
                int msgSize = messageVectorOut.size();
                //qDebug()<<"msgSize"<<msgSize;
                //qDebug()<<"messageView->message.size()"<<messageView->message.size();
                if(messageView->message.size() != msgSize)
                {
                    messageView->message.resize(msgSize);
                    updateFLag = true;
                }
                if(msgSize>0 && messageView->message.size() == msgSize)   //有消息
                {
                    for(int i = 0;i<msgSize;i++)
                    {
                        //qDebug()<<i<<":"<<globalData->messageVector[i].time;
                        if(messageView->message[i].messageCode != messageVectorOut[i].messageCode
                                ||messageView->message[i].MessageLevel != messageVectorOut[i].MessageLevel
                                ||messageView->message[i].time != messageVectorOut[i].time)
                        {
                            messageView->message[i] = messageVectorOut[i];

                            updateFLag = true;
                        }

//                        if(0 == messageView->message[i].time)
//                        {
//                             time_t tt = time(NULL);
//                             messageView->message[i].time = tt;
//                        }

                        //qDebug()<<globalData->messageVector[i].MessageLevel;
                        //qDebug()<<QString::fromStdString(globalData->messageVector[i].time);
                        //qDebug()<<QString::fromStdString(globalData->messageVector[i].componentName);
                    }
                }
            }
//            globalData->unlock();
//        }
    }

    if(messageView->message.size() >= 0 && updateFLag)//messageView->message.size() >= 0
    {
        //qDebug()<<"message change";
        messageView->updateView();                              //有消息,才刷新
        updateFLag = false;
    }
}

void MessageDelegate::clearMessageSlot(int messageIndex)
{
    if(messageIndex>=0)
    {
        emit clearMsgSignal(messageIndex);//messageView->message[i].messageCode);
    }

    //qDebug() << "clear line " << messageIndex << "message";
}

void MessageDelegate::clearAllMessageSlot()
{
    emit clearAllMsgSignal();

    //qDebug() << "clear all message";
}

void MessageDelegate::errorMessageShowOrHideSlot(int errorFlag)
{
    if(1 == errorFlag)
    {
        messageView->errorFlag = 0;
        //qDebug() << "error Message Hide";
    }
    else if(0 == errorFlag)
    {
        messageView->errorFlag = 1;
        //qDebug() << "error Message Show";
    }

    messageView->updateView();
}

void MessageDelegate::warnMessageShowOrHideSlot(int warnFlag)
{
    if(1 == warnFlag)
    {
        messageView->warnFlag = 0;
        //qDebug() << "warn Message Hide";
    }
    else if(0 == warnFlag)
    {
        messageView->warnFlag = 1;
        //qDebug() << "warn Message Show";
    }

   messageView->updateView();
}

void MessageDelegate::noteMessageShowOrHideSlot(int noteFlag)
{
    if(1 == noteFlag)
    {
        messageView->noteFlag = 0;
        //qDebug() << "note Message Hide";
    }
    else if(0 == noteFlag)
    {
        messageView->noteFlag = 1;
        //qDebug() << "note Message Show";
    }

    messageView->updateView();
}

void MessageDelegate::waitMessageShowOrHideSlot(int waitFlag)
{
    if(1 == waitFlag)
    {
        messageView->waitFlag = 0;
        //qDebug() << "wait Message Hide";
    }
    else if(0 == waitFlag)
    {
        messageView->waitFlag = 1;
        //qDebug() << "wait Message Show";
    }

    messageView->updateView();
}

void MessageDelegate::showCurrentRobotMessageSlot()
{
    messageView->showType = 0;
    //qDebug() << "show" << robotName << "Message";
    messageView->updateView();
}

void MessageDelegate::showAllRobotMessageSlot()
{
    messageView->showType = 1;
    //qDebug() << "show All Robot Message";
    messageView->updateView();
}
